package com.example.graduation.controller.chat;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.graduation.entity.pojo.Chat;
import com.example.graduation.service.ChatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Administrator
 */
@RequestMapping("/chat")
@Slf4j
@Api(tags = "带chat路径的Controller")
public class ChatController {
    private static final Integer PAGE_SIZE = 10;

    @Resource
    private ChatService chatService;

    /**
     * 分页查询聊天记录
     *
     * @param pageNumber 当前页数
     * @return PageInfo
     */
    @PostMapping("/listPages/{pageNumber}")
    @ResponseBody
    public PageInfo listPages(@PathVariable("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();
        wrapper.orderByAsc("chat_create_time");
        List<Chat> list = chatService.list(wrapper);
        log.info("list size() : {}", list.size());
        return new PageInfo<>(list);
    }

    /**
     * 根据聊天记录ID获取分页聊天记录
     *
     * @param chatId 聊天记录ID
     * @return PageInfo
     */
    @PostMapping("/getChatPages/{pageNumber}/{chatId}")
    @ResponseBody
    public PageInfo getChatPages(@PathVariable("chatId") String chatId,
                                 @PathVariable("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();
        wrapper.eq("chat_id", chatId);
        wrapper.orderByAsc("chat_create_time");
        List<Chat> list = chatService.list(wrapper);
        log.info("list size() : {}", list.size());
        return new PageInfo<>(list);
    }

    /**
     * 通过userId模糊查询聊天记录
     *
     * @param userId 用户ID
     * @return PageInfo
     */
    @PostMapping("/getChatList/{pageNumber}/{userId}")
    @ResponseBody
    public PageInfo getChatList(@PathVariable("userId") String userId,
                                @PathVariable("pageNumber") Integer pageNumber) {
        PageHelper.startPage(pageNumber, PAGE_SIZE);
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();
        wrapper.like("chat_send_user_id", userId);
        wrapper.like("chat_accept_user_id", userId);
        wrapper.orderByAsc("chat_create_time");
        List<Chat> list = chatService.list(wrapper);
        log.info("list size() : {}", list.size());
        return new PageInfo<>(list);
    }

    /**
     * 删除某一条聊天记录
     *
     * @param chatId 聊天记录ID
     * @return 删除结果
     */
    @PostMapping("/delChat/{chatId}")
    @ResponseBody
    public boolean delChat(@PathVariable("chatId") String chatId) {
        boolean result = true;
        QueryWrapper<Chat> wrapper = new QueryWrapper<>();

        return result;
    }
}
